package modes

import (
	"fmt"
)

type SetBlem struct {
	Problem `json:"blem" xorm:"extends"`
	// Resource  	`json:"ource" xorm:"extends"`
	StudentInfo `json:"info" xorm:"extends"`
}

func (this *SetBlem) Lookups(val *Pager) error {
	list := make([]SetBlem, 0)
	val.TName = "problem"
	val.Page--
	var field, sort string
	switch val.Coll {
	case 0:
		field = fmt.Sprintf("%s.at", val.TName) // 时间排序
	default:
		field = fmt.Sprintf("%s.at", val.TName) // 状态
	}
	if 1 == val.Rules {
		sort = fmt.Sprintf("%s asc", field)
	} else {
		sort = fmt.Sprintf("%s desc", field)
	}
	val.Total, _ = Db(0).Table(val.TName).Where(val.ToWhere()).Count(this)
	err := Db(0).Table(val.TName).
		// Join( "LEFT", "resources", fmt.Sprintf( "%s.rid = resources.id", val.TName ) ).
		Join("LEFT", "studen_info", fmt.Sprintf("%s.uid = studen_info.id", val.TName)).
		Where(val.ToWhere()).
		OrderBy(sort).
		Limit(val.Count, val.Page*val.Count).
		Find(&list)
	if err == nil {
		val.List = list
	}
	return err

}

type SetClassJect struct {
	Resource `json:"ource" xorm:"extends"`
	Project  `json:"ject" xorm:"extends"`
}

func (this *SetClassJect) Lists(val *Pager) error {
	list := make([]SetClassJect, 0)
	val.TName = "resources"
	val.Page--
	var field, sort string
	switch val.Coll {
	case 0:
		field = fmt.Sprintf("%s.at", val.TName) // 时间排序
	case 1:
		field = fmt.Sprintf("%s.pid", val.TName) // 项目
	case 2:
		field = fmt.Sprintf("%s.start", val.TName) // 状态
	default:
		field = fmt.Sprintf("%s.at", val.TName) // 时间排序
	}
	if 1 == val.Rules {
		sort = fmt.Sprintf("%s asc", field)
	} else {
		sort = fmt.Sprintf("%s desc", field)
	}
	val.Total, _ = Db(0).Table(val.TName).Where(val.ToWhere()).Count(this)
	err := Db(0).Table(val.TName).
		Join("INNER", "project", fmt.Sprintf("%s.pid = project.id", val.TName)).
		Where(val.ToWhere()).
		OrderBy(sort).
		Limit(val.Count, val.Page*val.Count).
		Find(&list)
	if err == nil {
		val.List = list
	}
	return err
}
